package com.eshop.frameworks.core.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PageEntity implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 3428610204086857703L;

	/** 总数据条数 */
	private int totalResultSize = 0;

	/** 总页数 */
	private int totalPageSize = 0;

	/** 每页条数 */
	private int pageSize = 10;

	/** 当前页码 */
	private int currentPage = 1;

	private int startRow = 0; // 当前页在数据库中的起始行
	private int startPage = 0;
	private int endPage = 0;
	private int showSize = 7;

	public PageEntity() {

	}

	public PageEntity(int _totalRows, int _pageSize) {
		totalResultSize = _totalRows;
		pageSize = _pageSize;
		totalPageSize = totalResultSize / pageSize;
		int mod = totalResultSize % pageSize;
		if (mod > 0) {
			totalPageSize++;
		}
		currentPage = 1;
		startRow = 0;

	}

	public PageEntity(int currentPage, int _totalRows, int _pageSize) {
		int totalPages1 = _totalRows / _pageSize;
		int mod1 = _totalRows % _pageSize;
		totalResultSize = _totalRows;
		pageSize = _pageSize;
		if (mod1 > 0) {
			totalPages1++;
		}
		if (currentPage > totalPages1) {
			currentPage = currentPage - 1;
		}
		if (currentPage == 0) {
			this.setStart(1);
		} else {
			this.setStart(currentPage);
		}
		totalPageSize = totalResultSize / pageSize;
		int mod = totalResultSize % pageSize;
		if (mod > 0) {
			totalPageSize++;
		} else {
			if (currentPage == 0) {
				currentPage = 1;
			}
		}
	}

	/** 是否首页 */
	@SuppressWarnings("unused")
	private boolean first = false;
	/** 是否尾页 */
	@SuppressWarnings("unused")
	private boolean last = false;

	public boolean isFirst() {
		return getCurrentPage() <= 1;
	}

	public void setFirst(boolean first) {
		this.first = first;
	}

	public boolean isLast() {
		return getCurrentPage() >= getTotalPageSize();
	}

	public void setLast(boolean last) {
		this.last = last;
	}

	/**
	 * 获得页码 List<Integer>
	 * @return
	 */
	public List<Integer> getPageNums() {
		List<Integer> returnList = new ArrayList<Integer>();
		int startNum = getCurrentPage() - getPageSize() < 1 ? 1 : getCurrentPage() - getPageSize();
		int endNum = getCurrentPage() + getPageSize() > getTotalPageSize() ? getTotalPageSize() : getCurrentPage() + getPageSize();
		for (int i = startNum; i <= endNum; i++) {
			returnList.add(i);
		}
		return returnList;
	}

	private void calculatePageNums() {
		int harf = (showSize + 1) / 2;
		this.startPage = getCurrentPage() > harf ? getCurrentPage() - harf + 1 : 1;
		this.endPage = (getCurrentPage() + harf - 1) < getTotalPageSize() ? (getCurrentPage() + harf - 1) : getTotalPageSize();
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 获取起始记录数
	 * @return
	 */
	public int getStartRecord() {
		return (currentPage - 1) * pageSize;
	}

	/**
	 * 获得结束记录数
	 * @return
	 */
	public int getEndRecord() {
		return getStartRecord() + pageSize - 1;
	}

	public int getTotalResultSize() {
		return totalResultSize;
	}

	public void setTotalResultSize(int totalResultSize) {
		this.totalResultSize = totalResultSize;
		this.catulateTotalPage();
	}

	public int getTotalPageSize() {
		return totalPageSize;
	}

	public void setTotalPageSize(int totalPageSize) {
		this.totalPageSize = totalPageSize;
	}

	public void setStart(int currentPage) {
		this.currentPage = currentPage;
		startRow = (currentPage - 1) * pageSize;
	}

	public int getStartRow() {
		return startRow;
	}

	public int getStartPage() {
		return startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setShowSize(int showSize) {
		this.showSize = showSize;
	}

	public void catulateTotalPage() {
		totalPageSize = totalResultSize / pageSize;
		int mod = totalResultSize % pageSize;
		if (mod > 0) {
			totalPageSize++;
		}
		if (currentPage > totalPageSize) {
			currentPage = totalPageSize;
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		startRow = (currentPage - 1) * pageSize > totalResultSize ? totalResultSize : (currentPage - 1) * pageSize;
		calculatePageNums();
	}

	public static PageEntity getPageEntity(int currentPage, int totalRows, int pageSize) {
		PageEntity pager = new PageEntity(totalRows, pageSize);
		if (currentPage == 0) {
			pager.setStart(1);
		} else {
			pager.setStart(currentPage);
		}
		return pager;
	}

	public static void main(String[] args) {
		PageEntity page = new PageEntity();
		page.setCurrentPage(9);
		page.setTotalResultSize(100);
		for (int i = page.getStartPage(); i <= page.getEndPage(); i++) {
			if (i == page.getCurrentPage()) {
				System.out.print("(" + i + ")  ");
			} else {
				System.out.print(i + "  ");
			}
		}
	}
}
